package com.faxsun.api.wrapper.customer;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.broadleafcommerce.profile.core.domain.Customer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.faxsun.profile.core.domain.FSCustomer;
import com.faxsun.profile.core.domain.FSCustomerImpl;
import com.faxsun.profile.core.domain.FSThirdPartyCustomer;

import javax.xml.bind.annotation.XmlAccessType;

@Component("fsCustomerWrapper")
@Scope("prototype")
@XmlRootElement(name = "fsCustomer")
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlType(name = "fsCustomerResource")
public class CustomerWrapper extends org.broadleafcommerce.core.web.api.wrapper.CustomerWrapper {

	@XmlElement
	protected String username;

	@XmlElement
	protected String phone;

	@XmlElement
	protected Boolean bindResult;

	@XmlElement
	protected List<String> openId;
	
	@XmlElement
	protected String unionId;
	
	@XmlElement
	protected String nickName;
	
	@XmlElement
	protected String avatar;
	
	@XmlElement
	protected Integer errorCode;

	@Override
	public void wrapDetails(Customer model, HttpServletRequest request) {
		super.wrapDetails(model, request);
		this.username = model.getUsername();
		this.phone = ((FSCustomerImpl) model).getMobile();
		super.lastName = null;
	}

	@Override
	public void wrapSummary(Customer model, HttpServletRequest request) {
		super.wrapSummary(model, request);
		this.firstName = null;
		this.lastName = null;
		this.emailAddress = null;
		this.customerAttributes = null;
		openId = new ArrayList<String>();
		this.bindResult = false;
	}

	public void wrapBindResult(Customer model, HttpServletRequest request, Boolean result) {
		this.id = model.getId();
		this.bindResult = result;
	}
	
	public void wrapBindResult(Customer model, HttpServletRequest request, Boolean result, int errorCode) {
		this.wrapBindResult(model, request, result);
		this.errorCode=errorCode;
	}

	public void wrapThirdAccount(List<FSThirdPartyCustomer> thirdAccount) {
		if (thirdAccount.size() > 0) {
			if (openId == null)
				openId = new ArrayList<String>();
			this.bindResult = true;
			for (FSThirdPartyCustomer account : thirdAccount) {
				openId.add(account.getUid() + account.getChannelId());
				if(account.getChannelId().equals("0004")){
					this.unionId=account.getUnionId();
					this.avatar=account.getAvatar();
					this.nickName=account.getNickName();
				}
			}
		}
	}

	@Override
	public Customer unwrap(HttpServletRequest request, ApplicationContext context) {
		FSCustomer customer = (FSCustomer) super.unwrap(request, context);
		customer.setUsername(this.username);
		customer.setMobile(this.phone);
		return customer;
	}

}
